package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<User> findAll() {
//        int i=1/0;
        return userDao.findAll();
    }

    /**
     * 分页插件
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<User> findByPage(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<User> list = userDao.findAll();
        return new PageInfo<>(list);
    }

    /**
     * 根据id查用户
     * @param id
     * @return
     */
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    /**
     * 保存用户
     * @return
     */

    @Override
    public int save(User user) {
        return userDao.save(user);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */

    @Override
    public int delteById(Integer id) {
        return userDao.delteById(id);
    }

    /**
     * 查询用户登入信息
     * @param username
     * @param password
     * @return
     */
    @Override
    public User findUserByUsernameAndPassword(String username, String password) {
       return userDao.findUserByUsernameAndPassword(username, password);
    }
}
